package com.das.mechanic_base.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.task.X3TaskUserAdapter;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.base.X3IBaseView;
import com.das.mechanic_base.bean.task.FollowCarAllBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.utils.X3StringUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class X3BottomTaskUserDialog extends X3BaseBottomSheetDialog implements View.OnClickListener, X3TaskUserAdapter.IOnTaskUserSelect, TextView.OnEditorActionListener, TextWatcher {

    private Button btn_cancel;
    private RecyclerView rlv_user;
    private Button btn_create;
    private EditText ed_input;
    private SmartRefreshLayout sl_view;
    private X3TaskUserAdapter taskUserAdapter;

    private FollowCarAllBean.ListBean userBean;

    public X3BottomTaskUserDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    private int pageNum = 1;

    @Override
    protected void initView() {
        btn_cancel  = findViewById(R.id.btn_cancel);
        rlv_user = findViewById(R.id.rlv_user);
        btn_create = findViewById(R.id.btn_create);
        ed_input = findViewById(R.id.ed_input);
        sl_view = findViewById(R.id.sl_view);

        sl_view.setEnableRefresh(false);
        sl_view.setEnableLoadMore(true);
        sl_view.setOnLoadMoreListener(refreshLayout -> {
            sl_view.finishLoadMore(500);
            pageNum++;
            String keyContent = ed_input.getText().toString();
            requestUser(keyContent,pageNum);
        });

        ed_input.setOnEditorActionListener(this);
        ed_input.addTextChangedListener(this);

        btn_cancel.setOnClickListener(this);
        btn_create.setOnClickListener(this);
        rlv_user.setLayoutManager(new LinearLayoutManager(mContext));
        taskUserAdapter = new X3TaskUserAdapter(mContext);
        taskUserAdapter.setiOnTaskUserSelect(this);
        rlv_user.setAdapter(taskUserAdapter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_task_user_bottom_dialog;
    }

    @Override
    public void show() {
        super.show();
        requestUser("",pageNum);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_cancel) {
            dismiss();
        } else if (id == R.id.btn_create) {
            if (userBean == null) {
                return;
            }
            if (iOnSearchUser != null) {
                iOnSearchUser.iOnSelectUser(userBean);
            }
            dismiss();
        }
    }

    IOnSearchUser iOnSearchUser;

    public void setiOnSearchUser(IOnSearchUser iOnSearchUser) {
        this.iOnSearchUser = iOnSearchUser;
    }

    @Override
    public void iOnTaskSlelect(FollowCarAllBean.ListBean userBean) {
        //选中的对象
        this.userBean = userBean;
    }

    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        if (i == EditorInfo.IME_ACTION_SEARCH) {
            String keyContent = ed_input.getText().toString();
            requestUser(keyContent,pageNum);

        }
        return true;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String keyContent = s.toString();
        if (iOnSearchUser != null) {
            pageNum = 1;
            requestUser(keyContent,pageNum);
        }
    }

    public interface IOnSearchUser {

        /**
         * 确认选中当前车主
         * @param userBean
         */
        void iOnSelectUser(FollowCarAllBean.ListBean userBean);
    }

    public void changeUser(List<FollowCarAllBean.ListBean> followCarBean) {
        userBean = null;
        if (X3StringUtils.isListEmpty(followCarBean)) {
            followCarBean = new ArrayList<>();
        }

        if (taskUserAdapter != null) {
            if (pageNum == 1) {
                taskUserAdapter.changeUser(followCarBean);
            }else {
                taskUserAdapter.changeAddUser(followCarBean);
            }
        }

        List<FollowCarAllBean.ListBean> listBeans = taskUserAdapter.getmList();
        sl_view.setEnableLoadMore(!X3StringUtils.isListEmpty(listBeans));

    }

    /**
     * 请求客户
     * @param keyWord
     * @param pageNum
     */
    public void requestUser(String keyWord,int pageNum) {
        NetWorkHttp.getApi().pageCarOwnerUser(keyWord,pageNum,20)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(((X3IBaseView)mContext).bindToLife())
                .subscribe(new HttpCallBack<FollowCarAllBean>() {
                    @Override
                    protected void onDone(FollowCarAllBean followCarBean) {
                        changeUser(followCarBean.getList());
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }
    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("待办事项我的客户弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("待办事项我的客户弹窗");
    }
}
